package org.jeecg.modules.lab.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.jeecg.modules.lab.device.entity.LabDevice;
import org.jeecg.modules.lab.device.mapper.LabDeviceMapper;
import org.jeecg.modules.lab.device.service.ILabDeviceService;
import org.jeecg.modules.lab.device.vo.LabDevRunPage;
import org.jeecg.modules.lab.device.vo.LabDevicePage;
import org.jeecg.modules.lab.utensil.entity.LabUtensil;
import org.jeecg.modules.lab.utensil.mapper.LabUtensilMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.util.List;

/**
 * @Description: 实验室设备信息
 * @Author: jeecg-boot
 * @Date:   2023-07-05
 * @Version: V1.0
 */
@Service
@Slf4j
public class LabDeviceServiceImpl extends ServiceImpl<LabDeviceMapper, LabDevice> implements ILabDeviceService {

    @Autowired
    private LabDeviceMapper labDeviceMapper;
    @Autowired
    private LabUtensilMapper labUtensilMapper;

    @Override
    public List<LabDevicePage> queryDeviceAndUtensil(String devName,String devNum,String status,String devType) {
        String startTime = String.valueOf(Year.now().atMonth(Month.JANUARY).atDay(1));
        String endTime =  String.valueOf(Year.now().atMonth(Month.DECEMBER).atEndOfMonth());
        return labDeviceMapper.selectDeviceAndUtensil(devName,devNum,status,devType,startTime,endTime);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAll(LabDevice labDevice) {
        labDeviceMapper.insert(labDevice);
        if(labDevice.getCheckMark().equals("1")){
            LabUtensil labUtensil =new LabUtensil();
            labUtensil.setUteName(labDevice.getDevName());
            labUtensil.setUteNumber(labDevice.getDevNumber());
            labUtensil.setManuNumber(labDevice.getInitialNumber());
            labUtensil.setSpecification(labDevice.getDevModel());
            labUtensil.setSort(labDevice.getAbcde());
            labUtensil.setPersonResponsible(labDevice.getCustodian());
            labUtensil.setStorageLocation(labDevice.getSavePlace());
            labUtensil.setFieldInspect(labDevice.getCheckMark());
            Integer checkCycle = labDevice.getCheckCycle();
            if (checkCycle != null) {
                labUtensil.setInspectCycle(Integer.valueOf(checkCycle));
            } else {
                // 当检查周期为null时，设置一个默认值或其他处理逻辑
                labUtensil.setInspectCycle(0);  // 设置为默认值 0
                // 或者忽略该赋值，不进行操作
            };
            labUtensil.setLastInspectDate(labDevice.getLastCheckDate());
            labUtensil.setUtePicture(labDevice.getDevPicture());
            labUtensil.setInstructions(labDevice.getRulesDoc());
            labUtensil.setDeviceId(labDevice.getId());
            labUtensilMapper.insert(labUtensil);
        }
    }

    @Override
    public void updateAll(LabDevice labDevice, LabUtensil utensil) {
        labDeviceMapper.updateById(labDevice);
        if (utensil!=null){
            utensil.setUteName(labDevice.getDevName());
            utensil.setUteNumber(labDevice.getDevNumber());
            utensil.setManuNumber(labDevice.getInitialNumber());
            utensil.setSpecification(labDevice.getDevModel());
            utensil.setSort(labDevice.getAbcde());
            utensil.setPersonResponsible(labDevice.getCustodian());
            utensil.setStorageLocation(labDevice.getSavePlace());
            utensil.setFieldInspect(labDevice.getCheckMark());
            utensil.setLastInspectDate(labDevice.getLastCheckDate());
            utensil.setUtePicture(labDevice.getDevPicture());
            utensil.setInstructions(labDevice.getRulesDoc());
            utensil.setDeviceId(labDevice.getId());
            utensil.setSynFlag("1");
            labUtensilMapper.updateById(utensil);
        }
    }

    @Override
    public void updateStatus(String status, List<String> devIds) {
        labDeviceMapper.updateStatus(status,devIds);
    }


    @Override
    public List<LabDevRunPage> getSumDevRunTime(String devId,String devName,String devNum,String devModel,String status,String startTime,String endTime) {
        return labDeviceMapper.getSumDevRunTime(devId,devName,devNum,devModel,status,startTime,endTime);
    }
}
